package br.com.util;

import java.io.IOException;
import java.net.URI;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import br.com.model.web.Login;

public class Web 
{
	public static Web novaInstacia( )  
	{
		return new Web( );
	}
	
	public String get( String strLink ) throws ClientProtocolException, IOException
	{
		CloseableHttpClient httpclient = HttpClientBuilder.create( ).build( );
		
		HttpGet httpget = new HttpGet( strLink );
		
		CloseableHttpResponse response = httpclient.execute( httpget );
		
		String pagina = IOUtils.toString( response.getEntity( ).getContent( ) );
		
		if( pagina != null )
		{
			return pagina.replaceAll( "\r", "" ).replaceAll( "\n", "" );
		}
		else
		{
			return "";
		}
	}
	
	public String get( String strLink, CloseableHttpClient client ) throws ClientProtocolException, IOException
	{
		HttpGet httpget = new HttpGet( strLink );
		
		CloseableHttpResponse response = client.execute( httpget );
		
		return IOUtils.toString( response.getEntity( ).getContent( ) );
	}

	public CloseableHttpClient login( Login login ) throws Exception 
	{
        BasicCookieStore cookieStore = new BasicCookieStore( );
        
        CloseableHttpClient httpclient = HttpClients.custom( ).setDefaultCookieStore( cookieStore ).build( );
        try 
        {
            RequestBuilder reqBuilder = RequestBuilder.post( ).setUri( new URI( login.getuRL( ) ) );
            
            reqBuilder.addParameter( login.getLoginParam(), login.getUser( ) );
            reqBuilder.addParameter( login.getPassParam( ), login.getPass( ) );
            
            CloseableHttpResponse response = httpclient.execute( reqBuilder.build( ) );
            
            try 
            {
                HttpEntity entity = response.getEntity( );

                EntityUtils.consume( entity );

                List<Cookie> cookies = cookieStore.getCookies( );
                
                if( cookies.isEmpty( ) )
                    System.out.println( "None" );
                
            } 
            finally 
            {
                response.close( );
            }
        }
        catch( Exception e ) 
        {
        	e.printStackTrace( );
        }
        
        return httpclient;
    }
	
	public static String filter( String strText )
	{
		if( strText == null )
			return strText;
		
		if( strText.contains( "&#82" ) )
			strText = strText.replace( "&#82;", "" );

		if( strText.contains( "&#36;" ) )
			strText = strText.replace("&#36;", "");
		
		return strText.trim( );
	}
	
	public static String cleanParans( String strText )
	{
		if( strText == null )
			return strText;
		
		if( strText.contains( "?" ) )
		{
			strText = strText.replaceAll( "\\?.*$", "" );
		}
		
		return strText.trim( );
	}
}